package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.OrderSettingDao;
import com.itheima.pojo.OrderSetting;
import com.itheima.pojo.Time;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    OrderSettingDao orderSettingDao;

    /**
     * 批量导入可预约数据
     */
    @Override
    public void editorderSettingList(List<OrderSetting> orderSettingList) {
        if(orderSettingList != null && orderSettingList.size()>0){
            for (OrderSetting orderSetting : orderSettingList) {

                //根据预约设置的日期查询数据库中的预约设置对象
                OrderSetting orderSettingDB =  orderSettingDao.findByDate(orderSetting.getOrderDate());

                //没有信息，直接添加
                if (orderSettingDB == null ){
                    orderSettingDao.add(orderSetting);
                }else {
                    if (orderSettingDB.getReservations() > orderSetting.getNumber()){

                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                        throw new RuntimeException("[ " +sdf.format(orderSetting.getOrderDate()) + "] 已预约人数大于可预约人数，批量导入失败!!!!");
                    }

                    //不大于，直接修改
                    orderSettingDao.edit(orderSetting);

                }

            }
        }

    }

    /**
     * 日历展示预约设置信息
     *      month:
     *         2019-11-01
     *         2019-11-30
     */
    @Override
    public List<OrderSetting> findByMonth(String month) {
        String startDate = month + "-01";
        String endDate = month + "-31";

        Map<String,String> map = new HashMap<>();
        map.put("startDate",startDate);
        map.put("endDate",endDate);

        List<OrderSetting> orderSettingList = orderSettingDao.findByBetweenDate(map);

        return orderSettingList;
    }

    /**
     * 定时删除数据
     * @param str
     */
    @Override
    public void JobDelete(String str) {
        orderSettingDao.JobDelete(str);
    }

    /**
     * 将时间添加到数据库
     * @param time
     */
    @Override
    public void DeleteTime(Time time) {
        System.out.println("哈哈哈哈哈哈哈");
        System.out.println(time.getDate());
        time.setId(1);
        orderSettingDao.DeleteTime(time);

    }

    /**
     * 查询定时数据
     * @return
     */
    @Override
    public Time findMonth() {
       return orderSettingDao.findMonth();
    }





}
